package com.sc.nft.dao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sc.nft.entity.SecondEquityProps;
import com.sc.nft.entity.vo.SecondConSignEquityPropsVO;
import com.sc.nft.entity.vo.secondEquityProp.MyConsignEquityPropsPageVO;
import com.sc.nft.entity.vo.secondEquityProp.SecondEquityPropBuyProductPageVO;
import com.sc.nft.entity.vo.secondEquityProp.SecondEquityPropConfPageVO;
import com.sc.nft.entity.vo.secondEquityProp.SecondEquityPropSearchConfVO;
import com.sc.nft.enums.ConSignStatus;
import com.sc.nft.mapper.SecondEquityPropsMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

/**
 * 二级-权益道具寄售表(sc_second_equity_props)数据DAO
 *
 * @author Sun
 * @since 2024-04-07 14:17:52
 * @description 由 Mybatisplus Code Generator 创建
 */
@Slf4j
@Repository
public class SecondEquityPropsDao extends ServiceImpl<SecondEquityPropsMapper, SecondEquityProps> {

    public boolean getIsExistByEquityPropsId(Long equityPropsId) {
        LambdaQueryWrapper<SecondEquityProps> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SecondEquityProps::getEquityPropsId,equityPropsId);
        wrapper.in(SecondEquityProps::getStatus,ConSignStatus.ON_SALES,ConSignStatus.LOCK);
        return count(wrapper) == 0;
    }

    public Page<SecondConSignEquityPropsVO> adminConSignPage(String userTel, Date startTime, int status, Date endTime, Page page) {
        return baseMapper.adminConSignPage(page,userTel,startTime,endTime,status);
    }

    public Page<MyConsignEquityPropsPageVO> myConsignEquityPropsPage(Long userId, Page page,Integer type) {
        return baseMapper.myConsignEquityPropsPage(page,userId,type);
    }

    public SecondEquityProps getUserEquityPropIdsByIdsAndUserId(Long userId, Long secondProductId) {
        LambdaQueryWrapper<SecondEquityProps> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SecondEquityProps::getUserId,userId);
        wrapper.eq(SecondEquityProps::getId,secondProductId);
        return getOne(wrapper);
    }

    public boolean cancelConsignBySecondProductId(Long userId, Long userEquityPropId, ConSignStatus status) {
        LambdaUpdateWrapper<SecondEquityProps> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(SecondEquityProps::getUserId,userId);
        wrapper.eq(SecondEquityProps::getId,userEquityPropId);
        wrapper.set(SecondEquityProps::getStatus,status);
        return update(new SecondEquityProps(),wrapper);
    }

    public boolean updateConsignStatusToLock(Long id){
        LambdaUpdateWrapper<SecondEquityProps> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(SecondEquityProps::getId,id);
        wrapper.eq(SecondEquityProps::getStatus,ConSignStatus.ON_SALES);
        wrapper.set(SecondEquityProps::getStatus,ConSignStatus.LOCK);
        return update(new SecondEquityProps(),wrapper);
    }


    public boolean updateLockStatusToSuccess(Long id,Date payTime) {
        LambdaUpdateWrapper<SecondEquityProps> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(SecondEquityProps::getId, id);
        wrapper.eq(SecondEquityProps::getStatus, ConSignStatus.LOCK);
        wrapper.set(SecondEquityProps::getStatus, ConSignStatus.SUCCESS);
        wrapper.set(SecondEquityProps::getSalesTime, payTime);
        return update(new SecondEquityProps(), wrapper);
    }
    public Page<SecondEquityPropSearchConfVO> searchProduct(String firstTitle, Page page) {
        return baseMapper.searchProduct(page,firstTitle);
    }

    public Page<SecondEquityPropConfPageVO> productPage(Page page) {
        return baseMapper.productPage(page);
    }

    public Page<SecondEquityPropBuyProductPageVO> getBuyProductPage(Page page,Long secondConfId,Integer sort) {
        return baseMapper.getBuyProductPage(page,secondConfId,sort);
    }

    public boolean updateStatusToCancel(Long secondEquityPropsId) {
        LambdaUpdateWrapper<SecondEquityProps> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(SecondEquityProps::getId,secondEquityPropsId);
        wrapper.eq(SecondEquityProps::getStatus,ConSignStatus.LOCK);
        wrapper.set(SecondEquityProps::getStatus,ConSignStatus.ON_SALES);
        return update(new SecondEquityProps(),wrapper);
    }

    public List<SecondEquityProps> getListByProductId(List<Long> productId) {
        LambdaQueryWrapper<SecondEquityProps> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(SecondEquityProps::getId,productId);
        wrapper.eq(SecondEquityProps::getStatus,ConSignStatus.ON_SALES);
        return list(wrapper);
    }
}